package org.grow.secure.controller;

import org.grow.secure.dao.UserInfoDao;
import org.grow.secure.entity.UserInfo;
import org.grow.secure.entity.UserRegDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Author: xwg
 * @CreateDate: 21-7-15
 */

@RestController
@RequestMapping("/regAbout")
public class RegController {

//    get   /regAbout/usernameExits?username=sudongpo08

    @Autowired
    private UserInfoDao userInfoDao;
    @GetMapping("/usernameExits")
    public Boolean usernameExits(@RequestParam String username){
        UserInfo user = userInfoDao.findByUsername(username);
        return user != null;
    }
    @Autowired
    private PasswordEncoder passwordEncoder;
//    /regAbout/register post
    @PostMapping("/register")
    public Boolean reg(@RequestBody UserRegDto userRegDto){
//        System.out.println("注册请求：" +userRegDto);
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(userRegDto,userInfo);
        userInfo.setPassword(passwordEncoder.encode(userRegDto.getPassword()));
        userInfo.setUserId(0);
        userInfo.setLastLoginTime(LocalDateTime.now());
        userInfo.setIsEnable(true);
        userInfoDao.save(userInfo);
        return true;
    }
}
